package com.example.lenovo.tianqi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.lenovo.tianqi.CityDB.db.DBManager;
import com.example.lenovo.tianqi.CityDB.db.WeatherDB;
import com.example.lenovo.tianqi.CityDB.domain.City;
import com.example.lenovo.tianqi.CityDB.domain.Province;
import com.example.lenovo.tianqi.R;
import com.example.lenovo.tianqi.adapter.CitySelectorAdapter;

import java.util.ArrayList;

/**
 * Created by lenovo on 2018/1/10.
 */

public class CitySelectorAvtivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private Toolbar toolbar;
    private ArrayList<Province> ProvinceList=new ArrayList<>();
    private ArrayList<City> CityList=new ArrayList<>();
    private ArrayList<String> data=new ArrayList<>();
    private Province selectedProvince;
    private CitySelectorAdapter citySelectorAdapter;
    public static final int SelectorSuccessFromMenu = 959;
    public static final int SelectorSuccessFromMultiCity = 960;
    private final int LEVEL_PROVINCE=1;//选择省份页面
    private final int LEVEL_CITY=2;//选择城市页面
    private int currentLEVEL;//当前的页面等级
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cityselector);
        initView();
        DBManager.getInstance().openDatabase();
        queryProvinces();
    }
    /**
     * 查询全国所有的省，从数据库查询
     */
    private void queryProvinces() {
        ProvinceList.addAll(WeatherDB.loadProvinces(DBManager.getInstance().getDatabase()));
        data.clear();
        for (Province province:ProvinceList){
            data.add(province.provinceName);
        }
        currentLEVEL=LEVEL_PROVINCE;
        if(citySelectorAdapter==null)
        {
            initAdapter();
            recyclerView.setLayoutManager(new LinearLayoutManager(this));////
            
        }
        else citySelectorAdapter.notifyDataSetChanged();
    }

    private void initAdapter() {
        citySelectorAdapter = new CitySelectorAdapter(R.layout.item_city_selector,data);
        citySelectorAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        citySelectorAdapter.isFirstOnly(false);
        recyclerView.setAdapter(citySelectorAdapter);
        citySelectorAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(currentLEVEL == LEVEL_PROVINCE)
                {
                    //点击了省份
                    selectedProvince = ProvinceList.get(position);
                    collapsingToolbarLayout.setTitle("选择城市");
                    queryCities();
                }
                else
                {
                    //点击了城市
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("CityName",CityList.get(position).mCityName);
                    intent.putExtras(bundle);
                    CitySelectorAvtivity.this.setResult(-1,intent);
                    quit();
                }
            }
        });
        toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CitySelectorAvtivity.this.onBackPressed();
            }
        });

    }

    private void quit() {

        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
//查询选中省份的所有城市，从数据库查询
    private void queryCities() {
        CityList.addAll(WeatherDB.loadCities(DBManager.getInstance().getDatabase(),selectedProvince.provinceSort));
        data.clear();
        for(City city:CityList)
        {
            data.add(city.mCityName);
        }
        currentLEVEL=LEVEL_CITY;
        citySelectorAdapter.notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(0);
    }

    private void initView() {
        recyclerView=findViewById(R.id.cityselector_RecyclerView);
        collapsingToolbarLayout=findViewById(R.id.cityselector_toolbar_layout);
        toolbar=findViewById(R.id.cityselector_toolbar);
    }

    @Override
    public void onBackPressed() {
        if(currentLEVEL==LEVEL_PROVINCE)
        {
            quit();
        }
        else
        {
            queryProvinces();
            CityList.clear();
            recyclerView.smoothScrollToPosition(0);
            collapsingToolbarLayout.setTitle("选择省份");
        }
    }

}
